<?php

namespace Home\Controller;

use Common\Controller\CommonController;

class UserController extends CommonController
{
    protected $article = null;

    public function _initialize()
    {
        $this->user = D('User');
        $this->role_user = D('Role');
    }

    //列表
    public function lists()
    {
        $opt = [];
        $page = I('page', 1);
        $pageSize = 20;
        $data = $this->user->getAllByLimit($opt, $page, $pageSize);

        $pageData['pageInfo'] = pagination($page, $data['count'], $pageSize);
        $this->assign('data', $data);
        $this->assign('pageData', $pageData);
        $this->display();
    }


    //新增
    public function add()
    {
        $data = $this->role_user->getAll();
        $this->assign('data',$data);
        $this->display();
    }


    public function add_callback()
    {
        $role_id_list = I('role_id');
        if (!$this->user->create()) {
            $this->ajaxReturn(array('msg' => $this->user->getError(), 'error_code' => 1));
        }
        $id = $this->user->add();
        if(!$id){
            $this->ajaxReturn(array('msg' => $this->user->getError(), 'error_code' => 1));
        }

        foreach($role_id_list as $key=>$value){
            $this->role_user->role_user_add($key, $id);
        }

        $this->ajaxReturn(array('msg' => '新增成功！', 'error_code' => 0));
    }

    public function test(){
        $this->role_user->role_user_add(4, 1);
        echo $this->role_user->getLastSql();
    }

    public function edit()
    {
        $id = I('id');
        $row = $this->user->where("id={$id}")->find();
        $this->assign('row', $row);
        $this->display();
    }

    public function edit_callback()
    {
        $id = I('id');
        $password = I('password');
        $role_id = I('role_id');
        if (!$this->user->create()) {
            $this->ajaxReturn(array('msg' => $this->user->getError(), 'error_code' => 1));
        }
        if($password){
            $this->user->password = md5($password);
        }

        $this->user->where("id={$id}")->save();
        $this->role_user->role_user_update($role_id, $id);
        $this->ajaxReturn(array('msg' => '修改成功！', 'error_code' => 0));
    }

    public function del_callback()
    {
        $id = I('id');
        $this->user->where("id={$id}")->delete();
        $this->ajaxReturn(array('msg' => '删除成功！', 'error_code' => 0));
    }


    public function uploadImage()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {
            $this->ajaxReturn(array('code' => 1, 'msg' => $upload->getError()));
        } else {
            foreach ($info as $k => $v) {
                $real_path = $v['savepath'] . $v['savename'];
            }
            $this->ajaxReturn(array('code' => 0, 'msg' => '成功', 'data' => array('src' => 'http://www.layui.test/Uploads/' . $real_path, 'title' => '土拨鼠')));
        }
    }

    //图片懒加载
    public function layimg()
    {
        $opt = [];
        $page = I('page', 1);
        $pageSize = 6;
        $data = $this->user->getAllByLimit($opt, $page, $pageSize);
        $this->assign('data',$data['data']);
        $this->display('img');
    }

    public function layimg_callback()
    {
        $opt = [];
        $page = I('page', 1);
        $pageSize = 2;
        $data = $this->user->getAllByLimit($opt, $page, $pageSize);
        $this->ajaxReturn(array('error_code' => 0, 'data' => $data['data'],'pages'=>5));
    }

}